package org.txazo.stat.web.interceptor;

import java.util.Map;

import org.txazo.stat.vo.User;
import org.txazo.stat.web.action.LoginAction;
import org.txazo.stat.web.action.RegisterAction;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * Session权限拦截器
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-13
 * 
 */
public class SessionInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		Map<String, Object> session = actionInvocation.getInvocationContext()
				.getSession();

		/** 登录时取消验证 */
		Action action = (Action) actionInvocation.getAction();
		if (action instanceof LoginAction || action instanceof RegisterAction) {
			return actionInvocation.invoke();
		}

		User user = (User) session.get("user");
		if (user == null) {
			return Action.LOGIN;
		}

		return actionInvocation.invoke();
	}

}
